<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="G1hRwH33Kv">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dockerlin666.gitee.com","root":"/","scheme":"Muse","version":"8.0.0-rc.5","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false};
  </script>

  <meta name="description" content="介绍shUnit2是一个xUnit单元测试框架，用于基于Bourne的shell脚本，它的工作方式与JUnit、PyUnit等类似。如果您曾经想为shell脚本编写单元测试，则可以使用shUnit2。shUnit2最初是为log4sh提供一致的测试解决方案而开发的，log4sh是一个基于shell的日志框架，类似于log4j。在该产品的开发过程中，一个反复出现的问题是，在一个shell(具体来说是">
<meta property="og:type" content="article">
<meta property="og:title" content="shell-shunit2">
<meta property="og:url" content="http://dockerlin666.gitee.com/2020/08/26/shell-shunit2/index.html">
<meta property="og:site_name" content="阳光灿烂的日志">
<meta property="og:description" content="介绍shUnit2是一个xUnit单元测试框架，用于基于Bourne的shell脚本，它的工作方式与JUnit、PyUnit等类似。如果您曾经想为shell脚本编写单元测试，则可以使用shUnit2。shUnit2最初是为log4sh提供一致的测试解决方案而开发的，log4sh是一个基于shell的日志框架，类似于log4j。在该产品的开发过程中，一个反复出现的问题是，在一个shell(具体来说是">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-26T08:12:25.000Z">
<meta property="article:modified_time" content="2020-09-08T03:26:51.459Z">
<meta property="article:author" content="刀客林">
<meta property="article:tag" content="shell">
<meta property="article:tag" content="unittest">
<meta property="article:tag" content="shunit2">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://dockerlin666.gitee.com/2020/08/26/shell-shunit2/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>shell-shunit2 | 阳光灿烂的日志</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?f59280177ba9fcb78a3c938de71308e3";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <main class="main">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader">
        <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">
      <img class="custom-logo-image" src="/uploads/logo400.png" alt="阳光灿烂的日志">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">阳光灿烂的日志</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">oh,my god</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
  </ul>
</nav>




</div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E7%AC%94%E8%AE%B0"><span class="nav-number">2.</span> <span class="nav-text">使用笔记</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%90%AD%E5%BB%BA%E7%8E%AF%E5%A2%83"><span class="nav-number">2.1.</span> <span class="nav-text">搭建环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8"><span class="nav-number">2.2.</span> <span class="nav-text">简单使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%9F%E8%83%BD%E6%BC%94%E7%A4%BA"><span class="nav-number">2.3.</span> <span class="nav-text">功能演示</span></a></li></ol></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="刀客林"
      src="/uploads/avatar.jpg">
  <p class="site-author-name" itemprop="name">刀客林</p>
  <div class="site-description" itemprop="description">记录无聊的生活，要不然都忘记了！</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">6</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </section>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </header>

      
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div id="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


      <div class="main-inner">
        

        <div class="content post posts-expand">
          

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dockerlin666.gitee.com/2020/08/26/shell-shunit2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.jpg">
      <meta itemprop="name" content="刀客林">
      <meta itemprop="description" content="记录无聊的生活，要不然都忘记了！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="阳光灿烂的日志">
    </span>

    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          shell-shunit2
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-08-26 16:12:25" itemprop="dateCreated datePublished" datetime="2020-08-26T16:12:25+08:00">2020-08-26</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-09-08 11:26:51" itemprop="dateModified" datetime="2020-09-08T11:26:51+08:00">2020-09-08</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/shell/" itemprop="url" rel="index"><span itemprop="name">shell</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2020/08/26/shell-shunit2/#SOHUCS" itemprop="discussionUrl">
        <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2020/08/26/shell-shunit2/" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.6k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><div class="note info"><p><strong>shUnit2是一个xUnit单元测试框架，用于基于Bourne的shell脚本，它的工作方式与JUnit、PyUnit等类似。如果您曾经想为shell脚本编写单元测试，则可以使用shUnit2。</strong><br>shUnit2最初是为log4sh提供一致的测试解决方案而开发的，log4sh是一个基于shell的日志框架，类似于log4j。在该产品的开发过程中，一个反复出现的问题是，在一个shell(具体来说是Linux上的/bin/bash)下一切正常，而在另一个shell (Solaris上的/bin/sh)下却不能正常工作。虽然运行了几个简单的测试，但它们是不够的，不能捕捉到一些极端情况。在发布了多个褐包版本之后，最终决定编写一个合适的单元测试框架。做了研究来寻找<br><a target="_blank" rel="noopener" href="https://github.com/kward/shunit2"><span style="color:red;">项目地址</span></a> <a target="_blank" rel="noopener" href="https://github.com/kward/shunit2"><span style="color:red;">手册</span></a></p>
</div>

<a id="more"></a>

<h2 id="使用笔记"><a href="#使用笔记" class="headerlink" title="使用笔记"></a>使用笔记</h2><h3 id="搭建环境"><a href="#搭建环境" class="headerlink" title="搭建环境"></a>搭建环境</h3><p>写本文的时候，版本是2.1.9pre</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git clone https:&#x2F;&#x2F;github.com&#x2F;kward&#x2F;shunit2</span><br><span class="line">mkdir test &amp;&amp; cd test</span><br><span class="line">cp ..&#x2F;shunit2&#x2F;shunit2 .</span><br></pre></td></tr></table></figure>

<h3 id="简单使用"><a href="#简单使用" class="headerlink" title="简单使用"></a>简单使用</h3><ul>
<li><p>编辑test.sh文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#! &#x2F;bin&#x2F;bash</span><br><span class="line"></span><br><span class="line">testEquality() &#123;</span><br><span class="line">    assertEquals 1 1</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># Load shUnit2.</span><br><span class="line">. .&#x2F;shunit2</span><br></pre></td></tr></table></figure></li>
<li><p>测试函数以test开始</p>
</li>
<li><p>执行脚本</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">.&#x2F;test.sh </span><br><span class="line">testEquality</span><br><span class="line"></span><br><span class="line">Ran 1 test.</span><br><span class="line"></span><br><span class="line">OK</span><br></pre></td></tr></table></figure>

<ul>
<li>下面我们模拟一下错误的情况<br>#! /bin/bash</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">testEquality() &#123;</span><br><span class="line">    assertEquals 2 1</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">testFail()&#123;</span><br><span class="line">    assertNotEquals  1 1</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># Load shUnit2.</span><br><span class="line">. .&#x2F;shunit2</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">.&#x2F;test.sh</span><br><span class="line">testEquality</span><br><span class="line">ASSERT:expected:&lt;2&gt; but was:&lt;1&gt;</span><br><span class="line">shunit2:ERROR testEquality() returned non-zero return code.</span><br><span class="line">testFail</span><br><span class="line">ASSERT:expected not same</span><br><span class="line">shunit2:ERROR testFail() returned non-zero return code.</span><br><span class="line"></span><br><span class="line">Ran 2 tests.</span><br><span class="line"></span><br><span class="line">FAILED (failures&#x3D;4)</span><br></pre></td></tr></table></figure>

<h3 id="功能演示"><a href="#功能演示" class="headerlink" title="功能演示"></a>功能演示</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#! &#x2F;bin&#x2F;bash</span><br><span class="line"></span><br><span class="line">oneTimeSetUp()&#123;</span><br><span class="line">    echo &quot;version is $&#123;SHUNIT_VERSION&#125;&quot;</span><br><span class="line">    echo &quot;oneTimeSetUp函数出现在所有测试的开始，一次测试只出现一次。&quot;</span><br><span class="line">    echo &quot;可以在这里声明全局环境变量（export xxx），加载全局测试文件（. .&#x2F;maketest.sh）&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">setUp()&#123;</span><br><span class="line">    echo &quot;setUp函数，每个测试方法前执行&quot;</span><br><span class="line">    echo &quot;可以在这里重新设置环境变量（env&#x3D;&#39;test&#39;）&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">tearDown()&#123;</span><br><span class="line">    echo &quot;tearDown函数，每个测试方法后执行。&quot;</span><br><span class="line">    echo &quot;可以在这里清理环境变量（unset xxx）&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">&#125;</span><br><span class="line">oneTimeTearDown()&#123;</span><br><span class="line">    echo &quot;oneTimeTearDown函数出现在所有测试结束后，一次测试只出现一次。&quot;</span><br><span class="line">    echo &quot;可以在这里清理全局环境变量（unset xxx），也可以使用shunit2提供的全局变量打印测试结果&quot;</span><br><span class="line">    echo &quot;--测试报告--&quot;</span><br><span class="line">    #echo &quot;用例是否成功0成功1失败 :: $&#123;__shunit_testSuccess&#125;&quot;</span><br><span class="line">    echo &quot;测试用例总数 :: $&#123;__shunit_testsTotal&#125;&quot;</span><br><span class="line">    echo &quot;通过用例数 :: $&#123;__shunit_testsPassed&#125;&quot;</span><br><span class="line">    echo &quot;失败用例数 :: $&#123;__shunit_testsFailed&#125;&quot;</span><br><span class="line">    echo &quot;总功能点 :: $&#123;__shunit_assertsTotal&#125;&quot;</span><br><span class="line">    echo &quot;通过功能点 :: $&#123;__shunit_assertsPassed&#125;&quot;</span><br><span class="line">    echo &quot;失败功能点 :: $&#123;__shunit_assertsFailed&#125;&quot;</span><br><span class="line">    echo &quot;跳过功能点 :: $&#123;__shunit_assertsSkipped&#125;&quot; </span><br><span class="line">    echo &quot;----------&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">testUnits()&#123;</span><br><span class="line">    echo &quot;单元测试方法，这里写具体的测试方法，比如如下测试用例：&quot;</span><br><span class="line">    echo &quot;测试相等 assertEquals(assertSame) &#39;不相等出现的说明文字时&#39; 1 1&quot;</span><br><span class="line">    assertEquals &#39;equals不相等出现的说明文字&#39; 1 1</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;在存在多个测试方法时，有时候我们需要知道错误的行号，可以使用$&#123;_ASSERT_EQUALS_&#125;来代替assertEquals，错误的时候回出现行号&quot;</span><br><span class="line">    $&#123;_ASSERT_EQUALS_&#125; &#39;equals不相等出现的说明文字&#39; 1 2</span><br><span class="line">    assertEquals --lineno &quot;$&#123;LINENO:-&#125;&quot; &#39;不相等出现的说明文字&#39; 1 3</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;测试不相等 assertNotEquals(assertNotSame) &#39;相等出现的说明文字&#39; 1 1&quot;</span><br><span class="line">    assertNotEquals &#39;equals相等出现的说明文字&#39; 1 1</span><br><span class="line">    assertNotSame &#39;same相等出现的说明文字&#39; 1 2</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;测试 assertNull &#39;不为NULL时出现&#39; 1 &quot;</span><br><span class="line">    assertNull &#39;不为NULL时出现&#39; 1 </span><br><span class="line">    assertNull &#39;空字符串出现&#39; &#39;&#39; </span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;测试 assertTrue &#39;不为true时显示&#39; &#39;[ 34 -gt 23 ]&#39;&quot;</span><br><span class="line">    assertTrue &#39;不为true时显示&#39; &quot;[ 34 -gt 23 ]&quot;</span><br><span class="line">    assertFalse &#39;为true时显示&#39; &quot;[ 34 -gt 23 ]&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &#39;测试fail &quot;fail message&quot;&#39;</span><br><span class="line">    assertTrue &#39;不为true时显示&#39; &quot;[ 34 -gt 23 ]&quot;</span><br><span class="line">    fail &quot;错误信息&quot;</span><br><span class="line">    failNotEquals &quot;错误信息&quot; &quot;no&quot; &quot;yes&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">testEnvs()&#123;</span><br><span class="line">    echo &quot;测试 SHUNIT_TRUE &#x3D; 0&quot;</span><br><span class="line">    assertEquals &#39;SHUNIT_TRUE不等于0&#39; $&#123;SHUNIT_TRUE&#125; 0</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;测试 SHUNIT_FALSE &#x3D; 1&quot;</span><br><span class="line">    assertEquals &#39;SHUNIT_FALSE 不等于1&#39; $&#123;SHUNIT_FALSE&#125; 1</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;测试 SHUNIT_ERROR &#x3D; 2&quot;</span><br><span class="line">    assertEquals &#39;SHUNIT_ERROR 不等于1&#39; $&#123;SHUNIT_ERROR&#125; 2</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;SHUNIT_TMPDIR &#x3D; $&#123;SHUNIT_TMPDIR&#125;&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;SHUNIT_COLOR &#x3D; $&#123;SHUNIT_COLOR&#125;&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;SHUNIT_TEST_PREFIX &#x3D; $&#123;SHUNIT_TEST_PREFIX&#125;&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;SHUNIT_TMPDIR &#x3D; $&#123;SHUNIT_TMPDIR&#125;&quot;</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">    echo &quot;在存在BASH_VERSION，跳过测试&quot;</span><br><span class="line">    [ -n &quot;$&#123;BASH_VERSION:-&#125;&quot; ] &amp;&amp; startSkipping</span><br><span class="line">    assertEquals &#39;equals不相等出现的说明文字&#39; 1 1</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">testSkipping()&#123;</span><br><span class="line">    startSkipping</span><br><span class="line">    assertEquals &#39;skipping 不显示 equals不相等出现的说明文字&#39; 1 2</span><br><span class="line">    if isSkipping; then</span><br><span class="line">        echo &quot;isSkipping目前的状态是跳过测试&quot;</span><br><span class="line">    fi</span><br><span class="line">    endSkipping</span><br><span class="line">    [ isSkipping ] &amp;&amp; echo &quot;当前状态是不跳过测试&quot;</span><br><span class="line">    assertEquals &#39;equals不相等出现的说明文字&#39; 1 1</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">. .&#x2F;shunit2</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/shell/" rel="tag"><i class="fa fa-tag"></i> shell</a>
              <a href="/tags/unittest/" rel="tag"><i class="fa fa-tag"></i> unittest</a>
              <a href="/tags/shunit2/" rel="tag"><i class="fa fa-tag"></i> shunit2</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/08/24/shell-log4jsh/" rel="prev" title="shell下记录日志log4jsh">
      <i class="fa fa-chevron-left"></i> shell下记录日志log4jsh
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/09/08/shell/" rel="next" title="shell">
      shell <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



        </div>
        
    
  <div class="comments">
    <div id="SOHUCS"></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="http://www.beian.miit.gov.cn/" rel="noopener" target="_blank">京ICP备 测试1234567890号-1 </a>
      <img src="http://www.beian.gov.cn/img/new/gongan.png" style="display: inline-block;"><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=%E6%B5%8B%E8%AF%951234567890" rel="noopener" target="_blank">京公网安备 测试1234567890号 </a>
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">刀客林</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">41k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">37 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>











<script>
document.querySelectorAll('.pdfobject-container').forEach(element => {
  let url = element.dataset.target;
  let pdfOpenParams = {
    navpanes : 0,
    toolbar  : 0,
    statusbar: 0,
    pagemode : 'thumbs',
    view     : 'FitH'
  };
  let pdfOpenFragment = '#' + Object.entries(pdfOpenParams).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&');
  let fullURL = `/lib/pdf/web/viewer.html?file=${encodeURIComponent(url)}${pdfOpenFragment}`;

  if (NexT.utils.supportsPDFs()) {
    element.innerHTML = `<embed class="pdfobject" src="${url + pdfOpenFragment}" type="application/pdf" style="height: ${element.dataset.height};">`;
  } else {
    element.innerHTML = `<iframe src="${fullURL}" style="height: ${element.dataset.height};" frameborder="0"></iframe>`;
  }
});
</script>


<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8.6.4/dist/mermaid.min.js', () => {
    mermaid.init({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    }, '.mermaid');
  }, window.mermaid);
}
</script>


  

  

  <script>
  NexT.utils.loadComments('#SOHUCS', () => {
    NexT.utils.getScript('https://changyan.sohu.com/upload/changyan.js', () => {
      window.changyan.api.config({
        appid: 'cyuYT0YXi',
        conf : '84caa3b92fd5f3996f4e117aad400242'
      });
    });
  });
  </script>
  <script src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>

</body>
</html>
